import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-03-22
 * Time: 8:58
 */
public class MyArrayList implements IList{
    int[] array = new int[10];
    private int usedSize = 0;

    @Override
    public void add(int data) {
        if(isExpansion()) {
           this.array = Expansion();
        }
        this.array[usedSize++] = data;
    }

    public void poslegal(int pos) throws PosillegalException{
        if(pos < 0 || pos > this.usedSize) {
            throw new PosillegalException("add的pos位置不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        try {
            poslegal(pos);
        }
        catch (PosillegalException p) {
            p.printStackTrace();
        }
        if(isExpansion()) {
            this.array = Expansion();
        }
        for(int i = usedSize - 1; i >= pos; i++) {
            this.array[i+1] = this.array[i];
        }
        this.array[pos] = data;
        this.usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for(int i = 0; i < this.usedSize; i++) {
            if(this.array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i = 0; i < this.usedSize; i++) {
            if(this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try{
            CheckSetorGet(pos);
        }catch (PosillegalException p2) {
            p2.printStackTrace();
        }
        return this.array[pos];
    }

    public void CheckSetorGet(int pos) throws PosillegalException{
        if(pos < 0 || pos >= this.usedSize) {
            throw new PosillegalException("SetorGet的pos位置不合法");
        }
    }
    @Override
    public void set(int pos, int value) {
        try{
            CheckSetorGet(pos);
        }catch (PosillegalException p1) {
            p1.printStackTrace();
        }
        array[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有找到要删除的数据!");
            return;
        }
        for(int i = index; i < this.usedSize - 1; i++) {
            array[i] = array[i+1];
        }
        this.usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        /*for(int i = 0; i < this.usedSize; i++) {
            this.array[i]=null;
        }*/
        this.usedSize = 0;
    }

    public void show() {
        for(int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }


    public int[] Expansion() {
        int[] array1 = Arrays.copyOf(array,2*array.length);
        return array1;
    }

    public boolean isExpansion() {
        if(this.usedSize == array.length) {
            return true;
        }
        return false;
    }



}
